<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * PENGUIN FrameWork
 * @author hungtd <tdhungit@gmail.com> 0972014011
 * @copyright Tran Dinh Hung 2011
 * 
 * Model
 * Function on User
 * 
 * @package PenguinFW
 * @subpackage User
 * @version 1.0.0
 */

class User extends MY_Model
{
    function __construct()
    {
        parent::__construct();
        
        $this->db_table = 'users';
    }
    
    /**
     * get all info user has id = $id
     * 
     * @param int $id id user
     * @return object account object
     */
    public function get_by_id($id)
    {
        return $this->db->get_where('users', array('id' => $id))->row();
    }
    
    /**
     * get all info user has username = $username
     * 
     * @param string $username username
     * @return object user object
     */
    public function get_by_username($username)
    {
        return $this->db->get_where('users', array('username' => $username))->row();
    }
    
    /**
     * get all info user has email = $email
     * 
     * @param string $email email of user
     * @return object user object
     */
    public function get_by_email($email)
    {
        return $this->db->get_where('users', array('email' => $email))->row();
    }
    
    /**
     * get all info user has typeid = $typeid
     * 
     * @param string $typeid type id (example: ZingID)
     * @return object user object 
     */
    public function get_by_typeid($typeid)
    {
        return $this->db->get_where('users', array('typeid' => $typeid))->row();
    }
    
    /**
     * create a user on Penguin System
     * 
     * @param array $userinfo 
     */
    public function create($userinfo)
    {
        if (empty ($userinfo)) return false;
        
        if (!$userinfo['username'] || !$userinfo['password']) return false;
        
        $this->load->helper('date');
        $this->load->helper('hashpasswd');
        
        $hashpass = new HashPasswd();
        $userinfo['password'] = $hashpass->Hash($userinfo['password']);
        
        $userinfo['created'] = mdate('%Y-%m-%d %H:%i:%s', now());
        $userinfo['modified'] = mdate('%Y-%m-%d %H:%i:%s', now());
        
        $this->db->insert('users', $userinfo);
        
        return $this->db->insert_id();
    }
    
    /**
     * Đăng nhập vào site với username
     * 
     * @param string $username username
     * @param string $password_type password do user gõ vào
     * @param bool $remember nhớ hay không. mặc định là không ghi nhớ tài khoản
     * @return boolean
     */
    public function login_username($username, $password_type, $remember = FALSE)
    {
        $this->load->helper('hashpasswd');
        $hashpasswd = new HashPasswd();

        $user = $this->get_by_username($username);
        
        if ($user && $hashpasswd->CheckPassword($user->password, $password_type) == TRUE) {                
            $session_data = array(
                'user_id' => $user->id,
                'user_username' => $user->username,
                'user_email' => $user->email,
                'user_typeid' => $user->typeid,
                'user_user_type_id' => $user->user_type_id,
                'user_user_level_id' => $user->user_level_id,
                'user_user_role_id' => $user->user_role_id
            );
            $this->session->set_userdata($session_data);
            
            return TRUE;
        }
        
        return FALSE;
    }
    
    /**
     * Đăng nhập vào site với email
     * 
     * @param string $email email
     * @param string $password_type password do user gõ vào
     * @param bool $remember nhớ hay không. mặc định là không ghi nhớ tài khoản
     * @return boolean
     */
    public function login_email($email, $password_type, $remember)
    {
        $this->load->helper('hashpasswd');
        $hashpasswd = new HashPasswd();

        $user = $this->get_by_email($email);
        
        if ($user && $hashpasswd->CheckPassword($user->password, $password_type) == TRUE) {                
            $session_data = array(
                'user_id' => $user->id,
                'user_username' => $user->username,
                'user_email' => $user->email,
                'user_typeid' => $user->typeid,
                'user_user_type_id' => $user->user_type_id,
                'user_user_level_id' => $user->user_level_id,
                'user_user_role_id' => $user->user_role_id
            );
            $this->session->set_userdata($session_data);
            
            return TRUE;
        }
        
        return FALSE;
    }
    
    /**
     * Đăng nhập vào site
     * 
     * @param string $userlogin
     * @param string $password_type
     * @param boolean $remember
     * @param string $config_userlogin 
     * @return boolean
     */
    public function login($userlogin, $password_type, $remember = FALSE, $config_userlogin = 'username')
    {
        if ($config_userlogin == 'email')
            return $this->login_email ($userlogin, $password_type, $remember);
        else return $this->login_username ($userlogin, $password_type, $remember);
    }
}

?>
